
local socket = require'socket'
local util = require'util'
local serialize = require'serialize'

local tests = {
  {file="client_small_string.lua",name='String 8'},
  {file="client_big_string.lua",name='String 2048'},
  {file="test_client_methods.lua",name='Methods'},
  {file="client_tab1.lua",name='Tab1'},
  {file="client_tab2.lua",name='Tab2'},
}
  
local latex_tab = {
  {"Teste","Fecha","1 cliente","média 3", "$\\sigma$ 3","média 10", "$\\sigma$ 10",}
}

local function run_nclients (filename,n,close)
  local client_array = {}
  local time_array = {}
  for i=1,n do
    client_array[i] = io.popen("lua "..filename..' '..(close and 'close' or ''),'r')
  end
  for i=1,n do
    time_array[i] = tonumber(client_array[i]:read())
  end
  return util.StandardDeviation(time_array)
end

local function insert_num (tab, num)
  table.insert(tab,num-num%0.00001)
end

local function make_line (test,close)
  print(test.name,(close and 'close' or 'open'))
  local line = {test.name,close and 'Sim' or 'Não'}
  --socket.sleep(2)
  local t1 = tonumber(io.popen("lua "..test.file..' '..(close and 'close' or ''),'r'):read())
  print('t1',t1)
  --socket.sleep(2)
  local dp3,t3 = run_nclients(test.file,3,close)
  print('t3',t3,dp3)
  --socket.sleep(2)
  local dp10,t10 = run_nclients(test.file,10,close)
  print('t10',t10,dp10)
  insert_num(line,t1)
  insert_num(line,t3)
  insert_num(line,dp3)
  insert_num(line,t10)
  insert_num(line,dp10)
  return line
end

for i,t in ipairs(tests) do
  table.insert(latex_tab,make_line(t,false))
  table.insert(latex_tab,make_line(t,true))
end

print(serialize('t',latex_tab))
local tab_str = util.GenerateTexTab(latex_tab)
print(tab_str)


